<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.xwbt" clearable placeholder="新闻标题" style="width: 200px;" class="filter-item" />
      <el-button v-waves class="filter-item light-warning-btn" type="warning" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <div style="display:inline-block;" v-if="isPrivilege('xwggadd')">
        <router-link :to="'/xwgg/add'">
          <el-button v-waves class="filter-item light-primary-btn" style="margin-left: 10px;" type="primary" icon="el-icon-plus">
            新增
          </el-button>
        </router-link>
      </div>
    </div>
    <div class="contents_list">
      <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
        <el-table-column align="center" label="序号" width="100px">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column  align="center" label="新闻标题">
          <template slot-scope="scope">
            <span>{{ scope.row.xwbt }}</span>
          </template>
        </el-table-column>

        <el-table-column width="100px" align="center" label="发布状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.fbzt | fbztStyleFilter">
              <span>{{ scope.row.fbzt | fbztFilter }}</span>
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column width="150px" align="center" label="发布时间">
          <template slot-scope="scope">
            <span v-if="scope.row.fbsj!=null">{{ scope.row.fbsj | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>

        <el-table-column width="120px" align="center" label="发布人">
          <template slot-scope="scope">
            <span>{{ scope.row.fbrName }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="320" header-align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.fbzt === 0 && isPrivilege('xwggpublish')" size="mini" type="success" @click="handlePublish(scope.row)">发布新闻
            </el-button>
            <el-button v-if="scope.row.fbzt === 1 && isPrivilege('xwggpublish')" size="mini" type="warning" @click="handlePublish(scope.row)">取消发布
            </el-button>

            <!--<router-link :to="'/xwgg/edit/'+scope.row.id">
              <el-button style="margin-left: 10px" v-if="isPrivilege('xwggupdate')"  type="primary" size="mini">
                修改
              </el-button>
            </router-link>-->
            <el-button v-if="isPrivilege('xwggupdate')"  type="primary" size="mini" @click="updateXwggDetail(scope.row)">
              修改
            </el-button>
            <el-button v-if="isPrivilege('xwggdelete')" style="margin-left: 10px" type="danger" size="mini" @click="handleDelete(scope)">删除</el-button>
            <el-button type="info" size="mini" @click="showXwnr(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageIndex" :limit.sync="listQuery.pageSize" @pagination="getList" />
    </div>
    <el-dialog :visible.sync="dialogVisible" width="980px">
      <h4 class="caption" v-html="xwbt" />
      <div class="detail ql-snow ql-editor">
        <div class="tip">
          <span>发布人：{{ fbrName }}</span>
          <span>{{ fbsj | parseTime('{y}-{m}-{d}') }}</span>
        </div>
        <p v-html="xwnr" />
      </div>

      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchXwgg, delXwgg, publishXwgg, getXwgg } from '@/api/portal/xwgg'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive
import { getUserId } from '@/utils/auth'
import { isPrivilege } from '@/utils/constUtils'

export default {
  name: 'XwggList',
  components: { Pagination },
  directives: { waves },
  filters: {
    fbztFilter(status) {
      const statusMap = {
        0: '未发布',
        1: '已发布'
      }
      return statusMap[status]
    },
    fbztStyleFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      dialogVisible: false,
      listQuery: JSON.parse(localStorage.getItem('xwggQuery')) || {
        pageIndex: 1,
        pageSize: 20,
        xwbt: null
      },
      xwnr: '',
      xwbt: '',
      fbrName: '',
      fbsj: '',
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      if (!isPrivilege('xwggpublish')) {
        this.listQuery.fbzt = 1
      }
      this.listLoading = true
      searchXwgg(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
      localStorage.setItem('xwggQuery', JSON.stringify(this.listQuery))
    },
    isPrivilege(resourceCode) {
      return isPrivilege(resourceCode)
    },
    updateXwggDetail(row) {
      this.$router.push({ path: '/xwgg/edit/' + row.id })
    },
    showXwnr(row) {
      getXwgg({ id: row.id }).then(response => {
        this.xwbt = row.xwbt
        this.fbrName = row.fbrName
        this.fbsj = row.fbsj
        this.xwnr = response.data.xwnr
        this.dialogVisible = true
      })
    },
    handleFilter() {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    handleDelete({ $index, row }) {
      this.$confirm('确认删除?', '操作提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delXwgg({ id: row.id })
          this.list.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(err => {
          console.error(err)
        })
    },
    handlePublish(row) {
      const obj = Object.assign({}, row) // copy obj
      this.publish(obj.id)
    },
    publish(id) {
      publishXwgg({ id: id, fbr: getUserId() }).then(response => {
        const returnCode = response && response.returnCode
        if (this.Const.OK === returnCode) {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.getList()
        } else {
          this.$message({
            type: 'error',
            message: response && response.msg || '操作失败'
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .filter-container {
    background-color: #DCE5EA;
    padding: 15px;
  }

  .contents_list {
    padding-top: 10px;
  }
  .el-table__row td:nth-child(3) span{
    line-height:24px;
    display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp:1;overflow: hidden
  }
  el-scrollbar{
    display: flex;
    justify-content: space-around;
    padding: 0 10px;
  }
  el-scrollbar_wrap{
    overflow: scroll;
    width: 110%;
    height: 120%;
  }
</style>
<style>
  .detail{border-top:1px solid #BEBEBE;margin-top:30px;padding:10px 0 30px;}
  .detail .tip{text-align: right;margin-bottom:30px;}
  .detail .tip span{margin:0 10px;font-size:12px;color:#565656;}
  .detail .picture img{max-width:600px;}
  .detail p{font-size:16px;line-height:30px;margin-bottom:40px;text-indent: 2em;}
  .caption{font-size:28px;text-align: center;}
</style>
